﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PledgeList : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        grid.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(grid_NeedDataSource);
        download.DataBinding += new EventHandler(download_DataBinding);
        base.OnInit(e);
        if(!IsPostBack)
        {
            if (Request.QueryString["SchoolId"] != null)
            {
                try
                {
                    SchoolId = int.Parse(Request.QueryString["SchoolId"]);
                }
                catch
                {
                }
            }
        }
    }

    void download_DataBinding(object sender, EventArgs e)
    {
        download.DataSource = GetData();
    }

    void grid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        grid.DataSource = GetData();
    }

    private int? SchoolId 
    { 
        get { return ViewState["SchoolId"] as int?;}
        set { ViewState["SchoolId"] = value; }
    }


    protected IQueryable GetData()
    {
        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        var data1 = from pledge in context.Pledges
                        join user in context.Users on pledge.UserId equals user.Id into u
                    from user in u.DefaultIfEmpty()
                        join customprop in context.UserCustomProperties on user.Id equals customprop.UserId into ucp
                    from customprop in ucp.DefaultIfEmpty()
                    where customprop.Name == "School Name"
                    select new {pledge, user, customprop};
        
        if(SchoolId != null)
        {
            data1 = data1.Where(x => x.user.Id == SchoolId.Value);
        }

        var data = from d in data1
                   select new 
                   {
                       Id = d.user.Id,
                       Name = d.pledge.DisplayName,
                       Email = d.pledge.EmailAddress,
                       PledgeId = d.pledge.PledgeId,
                       SchoolName = d.customprop.Value,
                       Authenticated = d.pledge.Authenticated,
                       Created = DateTime.SpecifyKind(d.pledge.UtcCreated, DateTimeKind.Utc)
                   };
        return data;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    public override void GetExtaBreadCrumbs(Dictionary<string, string> extaItems)
    {
        if (SchoolId != null)
        {
            ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();

            string tmp = context.UserCustomProperties.Where(x => x.UserId == SchoolId && x.Name == "School Name").Select(x => x.Value).SingleOrDefault();
            if (!string.IsNullOrEmpty(tmp))
            {
                extaItems.Add(tmp, Request.Url.PathAndQuery);
            }
        }
    }
}
